import { App } from "../../application";
import { EidtPolyline, Point, Polygon } from "../../Engine";


/**
 * 多边形编辑
 */
export default class EitdPolygon {
    private app: App;
    private editWire: EidtPolyline;
    private lineClickListener;
    private lineClickFinishListener;
    private polygonParam;
    private polygon;
    private isOpen: boolean = false;

    /**
     * 多边形编辑
     * @example var lineData = {
            color: '#00FA9A',
            lineWidth: 2,  //线的宽度
        };
        let img = '/examples/resources/images/img_logo.39362dc1.png';
        var polygonData = {
                textureImg: img, //贴在多边形上的图片
                alphaValue: 1,  //透明值
                color: '#FF1493', //多边形的颜色
                transparent: true
        };
        function clickListener(point){ //鼠标左键点击的监听器
            console.log(point);
        }
        function rightClickListener(point){ //鼠标右键点击的监听器
            console.log(point);
        }
        var editPolygon = new GE.EitdPolygon();
        editPolygon.openEdit(lineData, polygonData, clickListener, rightClickListener); //打开多边形编辑功能
        //editPolygon.closeEdit(); //关闭多边形编辑
     */
    public constructor() {
        this.app = App.getInstance();
    }

    /**
     * 开启多边形编辑
     * @param lineParam 线的参数
     * @param lineParam.color 线的颜色
     * @param lineParam.lineWidth 线的宽度, 可选参数，默认为1
     * @param lineParam.depthTest 是否开启深度测试, 可选参数，默认为false
     * @param lineParam.transparent 是否开启透明, 可选参数，默认为false
     * @param polygonParam 多边形的参数
     * @param polygonParam.textureImg 贴在多边形上的图片
     * @param polygonParam.alphaValue 透明值
     * @param polygonParam.color 多边形的颜色
     * @param polygonParam.transparent 是否开启透明, 可选参数，默认为false
     * @param polygonParam.colorWrite 是否开启颜色写入, 可选参数，默认为true
     * @param polygonParam.depthTest 是否开启深度测试, 可选参数，默认为false
     * @param polygonParam.height 多边形的高度, 可选参数，默认为0
     * @param clickListener 点击时的监听器, 该监听有一个参数: point。它是点击的点
     * @param lineRightClickListener 结束时的监听事件, 该监听有三个参数: point, pointArray, polygon。 point是结束时点击的点； pointArray是多边形顶点数组； polygon是多边形对象
     * @example var lineData = {
            color: '#00FA9A',
            lineWidth: 2,  //线的宽度
        };
        let img = '/examples/resources/images/img_logo.39362dc1.png';
        var polygonData = {
                textureImg: img, //贴在多边形上的图片
                alphaValue: 1,  //透明值
                color: '#FF1493', //多边形的颜色
                transparent: true
        };
        function clickListener(point){ //鼠标左键点击的监听器
            console.log(point);
        }
        function rightClickListener(point, pointArray, polygon){ //鼠标右键点击的监听器
            console.log(point);
        }
        editPolygon.openEdit(lineData, polygonData, clickListener, rightClickListener); 
     */
    public openEdit(lineParam: {color?: string, lineWidth?: number, depthTest?: boolean, transparent?: boolean}, polygonParam:{textureImg?: string, alphaValue?: number, color?: string, transparent?:boolean, colorWrite?: boolean, depthTest?: boolean, height?:number}, clickListener?, lineRightClickListener?) {
        if (this.isOpen) {
            return;
        }

        this.polygonParam = polygonParam;
        this.lineClickListener = clickListener;
        this.lineClickFinishListener = lineRightClickListener;
        this.editWire = new EidtPolyline();
        lineParam['isCricleLine'] = true;
        this.editWire.openEdit(lineParam, this.leftClickListener.bind(this), this.rightClickListener.bind(this));

        this.isOpen = true;
    }

    /**
     * 关闭多边形编辑
     * @example editPolygon.closeEdit();
     */
    public closeEdit() {
        if(this.editWire){
            this.editWire.closeEdit();
            this.editWire = null;
        }
       
        this.isOpen = false;
        if(this.polygon){
            this.app.remove(this.polygon);
        }
    }

    private leftClickListener(point: Point){
        if(this.lineClickListener){
            this.lineClickListener(point);
        }
    }

    private rightClickListener(point: Point){
        if(!this.editWire){
            return;
        }
        this.editWire.closeEdit();

        let wire = this.editWire.getWire();
        let pointArray = wire.getPointArray();
        if(!this.polygonParam){
            this.polygonParam = {pointArray: pointArray};
        }else{
            this.polygonParam.pointArray = pointArray;
        }
        this.polygon = new Polygon(this.polygonParam);
        this.polygon.renderOrder = Infinity;
        this.app.add(this.polygon)
        if(!this.polygon.isBuildPolygonSuccess){
            this.isOpen = false;
        }

        if(this.lineClickFinishListener){
            this.lineClickFinishListener(point, pointArray, this.polygon);
        }
    }

}